import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.HashMap;

public class RMI_ClientService extends UnicastRemoteObject implements RMI_ClientInterface
{
    private static RMI_ClientGUI view;
    private String ip;
    
    public RMI_ClientService() throws RemoteException
    {
        try
        {
            ip = InetAddress.getLocalHost().getHostAddress();
        }
        catch(Exception e){}
    }

    @Override
    public void logged(String username) throws RemoteException 
    {
        view.setUsername(username);
        view.logged(true);
    }
    @Override
    public void setUsername(String username) throws RemoteException 
    {
        view.setUsername(username);
    }

    @Override
    public String getUsername() throws RemoteException 
    {
        return view.getUsername();
    }
    @Override
    public String getIP() throws RemoteException 
    {
        return ip;
    }
    @Override
    public void postResponse(String information) throws RemoteException 
    {
        view.postResponse(information);
    }
    @Override
    public void postAuthenticationError(String information) throws RemoteException 
    {
        view.postAuthenticationError(information);
    }
    @Override
    public void update(ArrayList<RepFile> files, ArrayList<String> users, HashMap<String, ArrayList<String>> editCopies, HashMap<String, ArrayList<String>> readCopies) throws RemoteException 
    {
        view.updateOnlineUsers(users, editCopies, readCopies);
        view.updateRepositoryFiles(files);
    }
    @Override
    public void fileDeleted(String fileName) throws RemoteException
    {
        view.fileDeleted(fileName);
    }
    @Override
    public void fileUpdated(String fileName) throws RemoteException 
    {
        view.fileUpdated(fileName);
    }
    
    public static void main(String[] args)
    {
        String directory = "C:/Cliente/";
        String address = "localhost";
        String registration;
        RMI_ClientService client;
        RMI_ServerInterface serverService;
        
        
        if (args.length == 1) //temos o directorio
        {
            directory = args[0];
        }
        if (args.length != 2) //temos de usar o multicast para saber endereço do servidor
        {
            try
            {
                DatagramSocket socket;
                DatagramPacket packet;
                
                socket = new DatagramSocket();
                socket.setSoTimeout(Constants.SOCKET_TIME);
                packet = new DatagramPacket(Constants.REQUEST.getBytes(), Constants.REQUEST.length(), InetAddress.getByName(Constants.UDP_MULTICAST), Constants.UDP_PORT);
            
                socket.send(packet);

                packet.setData(new byte[Constants.DATAGRAM_LENGHT]);
                packet.setLength(Constants.DATAGRAM_LENGHT);
                socket.receive(packet);      

                address = new String(packet.getData(),0,packet.getLength());
                System.out.println(address);
            }
            catch(Exception e){}
        }
        else
        {
            directory = args[0];
            address = args[1];
        }
        
        try
        {
            client = new RMI_ClientService();
            registration = "rmi://" + address + "/ServerService";
            serverService = (RMI_ServerInterface)Naming.lookup(registration);
            view = new RMI_ClientGUI(serverService, client, directory);
        }
        catch(RemoteException | NotBoundException | MalformedURLException e){}
    }
}
